package com.hz.web.service.impl;

import java.io.File;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.hz.web.dao.CompanyDao;
import com.hz.web.entity.CompanyEntity;
import com.hz.web.service.IcompanyService;
import com.hz.web.util.Constant;
import com.hz.web.util.Excel;
import com.hz.web.util.Uid;
import com.hz.web.util.UtilMethods;

/**
 * 企业信息imp层
 * @author Administrator
 *
 */
@Service
public class CompanyImpl implements IcompanyService {

	@Resource
	private CompanyDao companyDao;
	
	/**
	 * 查询企业信息
	 * @param parmsMap
	 * @return List<CompanyEntity>
	 */
	@Override
	public List<CompanyEntity> queryCompany(Map<String, Object> parmsMap) {
		
		return companyDao.queryCompany(parmsMap);
	}

	/**
	 * 查询企业信息总条数
	 * @param parmsMap
	 * @return int
	 */
	@Override
	public int queryCompanyCount(Map<String, Object> parmsMap) {
		
		return companyDao.queryCompanyCount(parmsMap);
	}

	/**
	 * 增加企业信息
	 * @param entity
	 * @return int
	 */
	@Override
	public int addCompany(CompanyEntity entity) {
		if(entity==null){
			return -1;
		}
		entity.setSysId(Uid.getUid());
		return companyDao.addCompany(entity);
	}

	/**
	 * 修改企业信息
	 * @param entity
	 * @return int
	 */
	@Override
	public int updateCompany(CompanyEntity entity) {
		
		return companyDao.updateCompany(entity);
	}

	/**
	 * 解析excle
	 */
	@Override
	public Map<String, Object> fileLoadExcle(String url, String commitmentCode,
			String departmentCode, HttpServletRequest request)
			throws ParseException {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("code","200");
		// ------------------------------------------------------------
		String path = request.getSession().getServletContext().getRealPath("");
		path = path + File.separator + "them" + File.separator
				+ "fileload_excle";
		String filePath = path + File.separator + url;
		// 从excl表格中读取数据
		Excel poi = new Excel();
		List<List<String>> list = poi.read(filePath);
		List<CompanyEntity> resultList = new ArrayList<CompanyEntity>();
		int hang = 0;
		for (List<String> list2 : list) {// 行
			hang++;
			List<String> cellList = list2; // 列
			CompanyEntity entity = new CompanyEntity();
			for (int i = 0; i < cellList.size(); i++) {
				
				int lie = i + 1;
				if (lie > 2) {
					break;
				}
				String value = cellList.get(i);
				if(value!=null){
					value = value.trim();
				}
				if (hang == 1) {
					//如果对应的列名不对，就删除，说明上传错文件了
					if (lie == 1) {
						if(!Constant.EXCLE_COMPANY_NAME.equals(value)){
							resultMap.put("code","300");
							UtilMethods.delFile(filePath);
							return resultMap;
						}
					}
					if (lie == 2) {
						if(!Constant.EXCLE_COMPANY_DATE.equals(value)){
							resultMap.put("code","300");
							UtilMethods.delFile(filePath);
							return resultMap;
						}
					}
				}
				if(hang==1){
					break;
				}
				if (lie == 1) {
					entity.setCompanyName(value);// 企业名称
				}else if (lie == 2) {
					entity.setCompanyCommitmentTime(value);// 承诺时间
				}
			}
			if(entity.getCompanyName()!=null && !"".equals(entity.getCompanyName().trim())){
				entity.setSysId(Uid.getUid());
				entity.setCommitmentCode(commitmentCode);//承诺编码（暂存的ID，承诺没有编码）
				entity.setDepartmentCode(departmentCode);//部门编码（暂存ID,部门没有编码）
				resultList.add(entity);
			}
		}

		int count = -1;
		try {
			if(resultList.size()==0){
				count=0;
			}else{
				count = addBatchCompany(resultList);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			resultMap.put("count", count);
			UtilMethods.delFile(filePath);
		}
		return resultMap;
	}
	
	
	/***
	 * 批量插入企业信息
	 * @param list
	 * @return
	 */
	@Override
	public int addBatchCompany(List<CompanyEntity> list) {
		
		return companyDao.addBatchCompany(list);
	}

}
